import menuList from "@/layout/sidebar-menu";
/** 全局权限判断函数，和指令 v-permission 功能类似 */
export const checkPermission = (value) => {
    if (Array.isArray(value) && value.length > 0) {
        let roles = localStorage.getItem('ms_keys')
        roles = roles ? JSON.parse(roles) : [];
        let list = value.filter(v => roles.includes(v));
        return list.length === value.length;
    } else {
        return false
    }
}

// 进入有权限菜单且存在二级菜单的第一个子菜单
export const goToHomeMenu = () => {
    let indexMenu = menuList.findIndex((v) => {
        return (
            checkPermission([v.permiss]) && v.subs.some((i) => checkPermission([i.permiss]))
        );
    });
    if (indexMenu > -1) {
        let childList = menuList[indexMenu].subs.filter((v) =>
            checkPermission([v.permiss])
        );
       return childList[0].index;
    } else {
        return '/403'
    }
}
